/***************************************************************************
 * main.cpp
 * Copyright (C) 2010  Jared Bell
 *
 * This file is part of FreeESP.
 *
 * FreeESP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FreeESP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FreeESP.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Credits:
 * + compudaze <compudaze@gmail.com> - FreeESP author.
 * + SuperNovaAO <itshax.com> - posted his CoD6Wallhack180 source code &
 *   showed me how to access the console.
 * + R3B3L <gamedeception.net forums> - posted how to disable recoil.
 ***************************************************************************/

#include <windows.h>
#include "memhackitemlist.h"
#include "getitems.h"
#include "memhack.h"
#include "engine.h"

DWORD WINAPI LoopFunction(LPVOID lpvoid) {
  MemHackItemList noRecoil_items = GetNoRecoilItems();
  MemHackItemList esp_items = GetEspItems();
  MemHackItemList thermalVision_items = GetThermalItems();

  MemHack noRecoil(&noRecoil_items);
  MemHack esp(&esp_items);
  MemHack thermalVision(&thermalVision_items);

  while (true) {
    if (GetAsyncKeyState(VK_F5) & 1) {
      ForceHostEnable();
    } else if (GetAsyncKeyState(VK_F6) & 1) {
      ForceHostDisable();
    } else if (GetAsyncKeyState(VK_F7) & 1) {
      SetMinimumPlayers();
    } else if (GetAsyncKeyState(VK_F8) & 1) {
      ShowFpsToggle();
    } else if (GetAsyncKeyState(VK_F9) & 1) {
      noRecoil.Toggle();
    } else if (GetAsyncKeyState(VK_F10) & 1) {
      esp.Toggle();
    } else if (GetAsyncKeyState(VK_F11) & 1) {
      thermalVision.Toggle();
    } else if (GetAsyncKeyState(VK_F12) & 1) {
      // if ANYTHING is on, disable everything
      if (noRecoil.IsEnabled() || esp.IsEnabled() || thermalVision.IsEnabled()) {
        noRecoil.Disable();
        esp.Disable();
        thermalVision.Disable();
      } else {
        noRecoil.Enable();
        esp.Enable();
        thermalVision.Enable();
      }
    }
    Sleep(100);
  }
  return 0;
}

BOOL WINAPI DllMain(HMODULE hmodule, DWORD reason, LPVOID lpvoid) {
  if (reason == DLL_PROCESS_ATTACH) {
    DisableThreadLibraryCalls(hmodule);
    CreateThread(0, 0, LoopFunction, 0, 0, 0);
  }
  return TRUE;
}